package pub.guoxin.oops.auth.controller;

import com.google.common.base.Strings;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import pub.guoxin.oops.annotation.InjectUser;
import pub.guoxin.oops.auth.service.UserService;
import pub.guoxin.oops.auth.vo.RoleUserVo;
import pub.guoxin.oops.auth.vo.UserInfoChangeVo;
import pub.guoxin.oops.auth.vo.UserVo;
import pub.guoxin.oops.constants.OopsPathConstants;
import pub.guoxin.oops.enums.Authority;
import pub.guoxin.oops.model.User;
import pub.guoxin.oops.vo.ResultVo;
import pub.guoxin.oops.vo.ResultVoBuilder;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(OopsPathConstants.USER_PATH)
@PreAuthorize(value = "hasAnyRole('ADMIN,ROOT')")
public class UserController {

    @Autowired
    private ResultVoBuilder builder;
    @Autowired
    private UserService userService;

    @ApiOperation(value = "查询用户", notes = "查询用户")
    @GetMapping(value = "/", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo<UserVo> find(@InjectUser User user,String username) {
        return userService.one(user, username);
    }

    @ApiOperation(value = "分页查询用户", notes = "分页查询用户")
    @GetMapping(value = "/page", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo<Page<UserVo>> page(@InjectUser User user,
                                       @RequestParam(required = false) Authority authority,
                                       @RequestParam(required = false, defaultValue = "0") Integer pageNumber,
                                       @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        if (Strings.isNullOrEmpty(user.getCompanyId())) {
            return builder.buildBadRequestResultInfo("该用户尚未填写公司信息");
        }
        Page<UserVo> page = userService.page(user, authority, pageNumber, pageSize);
        return builder.buildSuccessResultInfo(page);
    }

    @ApiOperation(value = "获得子帐户", notes = "分页查询用户")
    @GetMapping(value = "/role-user", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo<List<RoleUserVo>> page(@InjectUser User user){
        List<RoleUserVo> users = userService.getRoleUser(user);
        return builder.buildSuccessResultInfo(users);
    }

    @ApiOperation(value = "删除用户", notes = "删除用户")
    @DeleteMapping(value = "/", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo delete(@InjectUser User user, String username) {
        return userService.delete(user, username);
    }

    @ApiOperation(value = "修改用户信息", notes = "修改用户信息")
    @PostMapping(value = "/info", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo update(@InjectUser User user, @RequestBody @Valid UserInfoChangeVo userChangeVo) {
        return userService.update(user, userChangeVo);
    }

    @ApiOperation(value = "添加子帐户", notes = "添加子帐户")
    @PostMapping(value = "/role-user", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo create(@InjectUser User user, @RequestBody @Valid UserInfoChangeVo userChangeVo) {
        return userService.create(user, userChangeVo);
    }

}
